/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

package net.zarubsys.unianalyzer.dataminers.lpt.gui.wizard;

import java.util.List;

import net.zarubsys.unianalyzer.dataminers.lpt.gui.parameterObject.PreviewData;
import net.zarubsys.unianalyzer.dataminers.lpt.gui.plugin.LPTGUIPlugin;
import net.zarubsys.unianalyzer.dataminers.lpt.pin.CustomizedPin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * LPTMeasuringPinNames
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@media-solutions.cz&quot;&gt;Lukas Zaruba&lt;/A&gt;, MEDIA SOLUTIONS CZECH REPUBLIC Ltd.
 * @version $Revision$ $Date$
 */
public class LPTMeasuringPinNames extends AbstractPageWithContext {
	
	private Label imageLabel;
	private Label offsetImage;
	private Button[] pinDescs = new Button[8];
	private Text[] pinNames = new Text[8];
	
	private final SelectionListener checkBoxListener = new CheckBoxListener();
	private final ModifyListener textBoxListener = new TextBoxListener();
	
	private boolean initSequence = false;

	protected LPTMeasuringPinNames(String pageName) {
		super(pageName);
	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(2, false));
		
		Composite imageComposite = new Composite(composite, SWT.NONE);
		imageComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
		imageComposite.setLayout(new GridLayout(1, false));
		
		imageLabel = new Label(imageComposite, SWT.NONE);
		GridData gd = new GridData(SWT.CENTER, SWT.TOP, false, false);
		gd.widthHint = 180;
		imageLabel.setLayoutData(gd);
		
		offsetImage = new Label(imageComposite, SWT.NONE);
		gd = new GridData(SWT.CENTER, SWT.TOP, false, false);
		gd.widthHint = 50;
		gd.heightHint = 180;
		offsetImage.setLayoutData(gd);
		
		Composite pinsComposite = new Composite(composite, SWT.NONE);
		pinsComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
		pinsComposite.setLayout(new GridLayout(2, false));
		createPins(pinsComposite);
		
		setControl(composite);
	}

	private void setImageData(PreviewData data) {
		imageLabel.setText(data.getImageDescription());
		offsetImage.setImage(LPTGUIPlugin.getDefault().getImage(data));
	}
	
	private void setPinData(List<CustomizedPin> pins) {
		int i = 0;
		for (CustomizedPin pin : pins) {
			Button pinButton = pinDescs[i];
			pinButton.setText(pin.getLabel() + Messages.getString("LPTMeasuringPinNames.0")); //$NON-NLS-1$
			boolean enabled = !pin.isReserved();
			boolean checked = pin.isChecked();
			pinButton.setSelection(checked);
			pinButton.setGrayed(!enabled);
			pinButton.setEnabled(enabled);
			Text pinText = pinNames[i];
			pinText.setEnabled(enabled && checked);
			pinText.setText(pin.getCustomizedName());
			i++;
		}
	}

	private void createPins(Composite pinsComposite) {
		for (int i = 0; i < 8; i++) {
			Button button = new Button(pinsComposite, SWT.CHECK);
			GridData gd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
			gd.widthHint = 150;
			button.setLayoutData(gd);
			button.addSelectionListener(checkBoxListener);
			pinDescs[i] = button;
			
			Text text = new Text(pinsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
			text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
			text.addModifyListener(textBoxListener);
			pinNames[i] = text;
		}
	}
	
	private void validate() {
		boolean oneOrMoreChecked = false;
		for (CustomizedPin cPin : getMeasurignSettings().getCustomizedPins()) {
			oneOrMoreChecked |= cPin.isChecked();
			if (cPin.getCustomizedName().length() > 8) {
				setUserOutput(Messages.getString("LPTMeasuringPinNames.1") + cPin.getCustomizedName() + Messages.getString("LPTMeasuringPinNames.2")); //$NON-NLS-1$ //$NON-NLS-2$
				return;
			}
		}
		if (!oneOrMoreChecked) {
			setUserOutput(Messages.getString("LPTMeasuringPinNames.errorOnePinChecked")); //$NON-NLS-1$
			return;
		}
		setUserOutput(null);
	}
	
	private class CheckBoxListener implements SelectionListener {

		public void widgetDefaultSelected(SelectionEvent e) {
			// ignored
		}

		public void widgetSelected(SelectionEvent e) {
			if (initSequence) return;
			for (int i = 0; i < pinDescs.length; i++) {
				if (pinDescs[i] == (e.getSource())) {
					Button b = pinDescs[i];
					boolean selection = b.getSelection();
					getMeasurignSettings().getCustomizedPins().get(i).setChecked(selection);
					pinNames[i].setEnabled(selection);
					break;
				}
			}
			validate();
		}
		
	}
	
	private class TextBoxListener implements ModifyListener {

		public void modifyText(ModifyEvent e) {
			if (initSequence) return;
			for (int i = 0; i < pinNames.length; i++) {
				if (pinNames[i] == (e.getSource())) {
					Text t = pinNames[i];
					getMeasurignSettings().getCustomizedPins().get(i).setCustomizedName(t.getText());
					break;
				}	
			}
			validate();
		}
		
	}

	public void setVisible(boolean visible) {
		if (visible) {
			initSequence = true;
			int index = getMeasurignSettings().getPortSettings().getPortOffset();
			setImageData(PreviewData.getPreviewData(index));
			setPinData(getMeasurignSettings().getCustomizedPins());
			initSequence = false;
		}
		super.setVisible(visible);
	}

}


/*
 * $Log$
 */